package com.shenfa.action.upload;


import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.shenfa.service.FodderService;
import com.shenfa.service.PaperService;
import com.shenfa.service.TemplateService;

import com.shenfa.util.ReadShowFile;

@SuppressWarnings("serial")
public class DownloadAction extends ActionSupport implements ServletResponseAware{
	
	private HttpServletResponse response;
	private boolean onLine=true;
	private String type;
	/**
	 * 要预览的文件ID
	 */
	private String id;
	private String filepath;
	private static Logger log=Logger.getLogger(DownloadAction.class);
	@Autowired
	private PaperService paperService;
	@Autowired
	private TemplateService templateService;
	@Autowired
	private FodderService fodderService;
	
	public String execute(){
		log.debug("fileid="+id+",type="+type);
		try {
			id=new String(id.getBytes("iso-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		
		//查询预印纸底板
		if("paper".equals(type)){
			filepath=paperService.show(id);
		}else if("temp".equals(type)){
			filepath=templateService.show(id);
		}else if("fodder".equals(type)){
			filepath=fodderService.show(id);
		}else{
			filepath=id;
		}
		ReadShowFile.read(filepath, response, onLine);
		return null;
	}
	
	public void setServletResponse(HttpServletResponse arg0) {
		this.response=arg0;
	}

	public void setOnLine(boolean onLine) {
		this.onLine = onLine;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setId(String id) {
		this.id = id;
	}
}
